const DeviceData = require('../models/DeviceData');
// 获取设备数据
exports.getData = async (req, res) => {
  // 从请求中获取 deviceId, type, startDate, 和 endDate 参数
  const { deviceId, type, startDate, endDate } = req.query;
  // 根据参数查询数据库
  const data = await DeviceData.findAll({
    where: {
      device_id: deviceId,
      param_type: type,
      recorded_at: {
        [Op.between]: [startDate, endDate]
      }
    },
    order: [['recorded_at', 'DESC']]
  });
  // 返回 JSON 数据
  res.json({
    labels: data.map(d => d.recorded_at),
    datasets: [{
      label: type,
      data: data.map(d => d.value)
    }]
  });
};
